$(function () {

    var edit_dialog =  $('#edit_dialog');
    var edit_form =  $('#edit_form');
    var course_datagrid =  $('#course_datagrid');

    //数据表格
    course_datagrid.datagrid({
        url:'/course/list.do',
        striped:true,
        singleSelect:true,
        pagination:true,
        fitColumns:true,
        rownumbers:true,
        toolbar:"#tb",
        columns:[[
            {field:'cb',checkbox:true},
            {field:'id',hidden:true,width:100},
            {field:'name',title:'课程名称',width:100,align:'center'},
            {field:'courseNo',title:'课程编码',width:100,align:'center',sortable:true}

        ]]


    });
    //end


    //按照课程编号排序
    course_datagrid.datagrid('sort',{
        sortName: 'courseNo',
        sortOrder: 'asc',
        sortOrder: 'desc',
        url:'/course/query.do'
    });

    //获取行的值

    //end




    var cmdObj = {
        //添加按钮添加课程
        add : function () {

            edit_dialog.dialog("setTitle","添加课程");

            edit_form.form("clear");

            edit_dialog.dialog("open");
        },
        //编辑按钮编辑课程
        edit : function () {

            var row = course_datagrid.datagrid("getSelected");
            if (!row){
                $.messager.alert("温馨提示","请选择要编辑的课程","info");
                return;
            }

            edit_dialog.dialog("setTitle","编辑课程");

            //在读取数据之前,清除缓存
            edit_form.form("clear");

            edit_form.form("load",row);

            edit_dialog.dialog("open");
        },

        //save按钮提交表单
        save : function () {

            var url = "";

            var id = $("#deptId").val();

            if(id){
                url = "/course/update.do";
            }else{
                url = "/course/save.do";
            }


            edit_form.form('submit', {
                url:url,
                success: function(data){
                    var data = eval('(' + data + ')');
                    if (data.success){
                        $.messager.confirm('温馨提示','操作成功',function(r){
                            edit_dialog.dialog("close");
                            course_datagrid.datagrid("load");
                        });
                    }else{
                        $.messager.alert("温馨提示",data.msg);
                    }
                }
            });
        },
        //编辑的关闭按钮功能
        cancel : function () {
            edit_dialog.dialog("close");
        },
        //删除课程
        remove : function () {
            var row = course_datagrid.datagrid("getSelected");
            if (!row){
                $.messager.alert("温馨提示","请选择要删除的课程","info");
                return;
            }

            $.messager.confirm('确认','您确认想要删除记录吗？',function(r){
                if (r){
                    $.get("/course/delete.do?id=" + row.id,function (data) {
                        if (data.success){
                            $.messager.confirm('温馨提示','删除成功',function(r){
                                course_datagrid.datagrid("load");
                            });
                        }else{
                            $.messager.alert("温馨提示",data.msg,"error");
                        }
                    })
                }
            });
        },
        //刷新
        reload :function () {
            course_datagrid.datagrid("reload");
        }

    }

    //抽取优化
    $("a").click(function () {
        var cmd = $(this).data("cmd");

        cmdObj[cmd]();
    })
    //end


    //dialog弹窗
    $('#edit_dialog').dialog({
        title: 'My Dialog',
        buttons:"#bb",
        closed:true,
        width: 350,
        height: 400,
    });
    //end

})//end













